package com.pansoft.openplanet.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class WalletDbHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "wallet.db";
    private static final int DB_VERSION = 16;


//    private  static final String CREATE_TABLE1="create table if not exists account1(address varchar(50) " +
//            ",im_userid varchar(20),account_name varchar(100),account_avatar varchar(500),account_filename varchar(200)," +
//            "create_time varchar(30) ,primary key(address,im_userid) )" ;

    private static final String CREATE_TABLE1 = "create table if not exists account1("
            + "address varchar(50), "
            + "im_userid varchar(20),"
            + "account_name varchar(100),"
            + "account_avatar varchar(500),"
            + "is_master INTEGER,"
            + "account_filename varchar(200),"
            + "public_key varchar(200) DEFAULT '',"
            + "secret_private_key varchar(200) DEFAULT '',"
            + "eos_active_pubkey varchar(300) DEFAULT '',"
            + "eos_active_prikey varchar(300) DEFAULT '',"
            + "secret_mnemonic_key varchar(500) DEFAULT '',"
            + "main_account_address varchar(200) DEFAULT '',"
            + "block_chain_name varchar(500) DEFAULT '',"
            + "account_type INTEGER default 0,"
            + "is_current_show INTEGER default 0,"
            + "enable INTEGER default 0,"
            + "create_time varchar(30),"
            + "eos_temp_name text DEFAULT '',"
            + "eos_account_names text DEFAULT '',"
            +"eos_is_activate integer DEFAULT 0,"
            + "cold_wallet_card_id varchar(30) DEFAULT '',"
            + "hd_btc_current_child_address varchar(30) DEFAULT '',"
            + "hd_btc_child_path_max_index varchar(10) DEFAULT '',"
            + "hd_btc_x_pub varchar(100) DEFAULT '',"
            + " primary key(address,im_userid) )";

    private static final String CREATE_CARDS_TABLE = "create table if not exists card("
            + "uid varchar(50), "
            + "version varchar(20), "
            + "card_type varchar(20), "
            + "user_id varchar(20), "
            + "wallet_info varchar(500), "
            + "enable INTEGER default 0,"
            + "primary key(uid, user_id) )";

    private static final String CREATE_CARDS_TABLE_OLD = "create table if not exists card("
            + "uid varchar(50), "
            + "version varchar(20), "
            + "card_type varchar(20), "
            + "user_id varchar(20), "
            + "wallet_info varchar(500), "
            + "primary key(uid) )";

    private static final String CREATE_CARDS_TABLE_COPY = "create table if not exists card_copy("
            + "uid varchar(50), "
            + "version varchar(20), "
            + "card_type varchar(20), "
            + "user_id varchar(20), "
            + "wallet_info varchar(500), "
            + "enable INTEGER default 0,"
            + "primary key(uid, user_id) )";

    private static WalletDbHelper walletDbHelper;

    WalletDbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    public static WalletDbHelper getInstance(Context context) {

        synchronized (WalletDbHelper.class) {
            if (null == walletDbHelper) {
                walletDbHelper = new WalletDbHelper(context.getApplicationContext());
            }
        }
        return walletDbHelper;
    }

    public SQLiteDatabase getDb() {
        return this.getWritableDatabase();
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE1);
        db.execSQL(CREATE_CARDS_TABLE);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (oldVersion < 2) {
            db.execSQL(CREATE_TABLE1);
            db.execSQL("INSERT INTO account1 SELECT * FROM account");
            db.execSQL("drop table  account");
        }
        if (oldVersion < 3) {
            //增加是否是主账号的字段
            db.execSQL("alter table account1 add is_master INTEGER DEFAULT 0;");
            //设置帐号为主账号
            db.execSQL("UPDATE  account1  set is_master= 1 ");

            db.execSQL("alter table account1 add public_key varchar(200) DEFAULT '';");
            db.execSQL("alter table account1 add secret_private_key varchar(200) DEFAULT '';");
            db.execSQL("alter table account1 add main_account_address varchar(200) DEFAULT '';");
            db.execSQL("alter table account1 add block_chain_name varchar(500) DEFAULT '';");

            db.execSQL("alter table account1 add account_type INTEGER DEFAULT 0;");
            db.execSQL("alter table account1 add is_current_show INTEGER DEFAULT 0;");

            //旧版本每个人只有一个 设置currentShow
            db.execSQL("UPDATE  account1  set is_current_show= 1 ");
            db.execSQL("UPDATE  account1  set block_chain_name= 'ETH' ");

        }
        if (oldVersion < 4) {
            //增加账户是否可用的字段
            db.execSQL("alter table account1 add  enable INTEGER DEFAULT 0;");
            //设置当前数据库账户都可用
            db.execSQL("UPDATE  account1  set enable= 1 ");

        }
        if (oldVersion < 5) {
            //增加加密助记词字段
            db.execSQL("alter table account1 add  secret_mnemonic_key varchar(500) DEFAULT '';");
        }
        if (oldVersion < 6) {
            //增加eos active 公钥私钥字段
            db.execSQL("alter table account1 add  eos_active_pubkey varchar(300) DEFAULT '';");
            db.execSQL("alter table account1 add  eos_active_prikey varchar(300) DEFAULT '';");
        }
        if (oldVersion < 7) {
            //增加eos isActivate 是否激活字段
            db.execSQL("alter table account1 add  eos_is_activate integer DEFAULT 0;");
        }
        if (oldVersion < 8) {
            //增加eos temp name 临时账户名
            db.execSQL("alter table account1 add  eos_temp_name text DEFAULT '';");
        }
        if (oldVersion < 9) {
            //增加eos account names 所有eos账户名
            db.execSQL("alter table account1 add  eos_account_names text DEFAULT '';");
        }
        if (oldVersion < 10) {

            //增加card卡片表
            db.execSQL(CREATE_CARDS_TABLE_OLD);
        }
        if (oldVersion < 12) {
            //增加cold_wallet_card_id字段，存储冷钱包所在卡片的uid
            db.execSQL("alter table account1 add cold_wallet_card_id varchar(30) DEFAULT '';");
        }
        if (oldVersion < 13) {
            //card表中增加enable字段
            db.execSQL("alter table card add enable integer DEFAULT 0;");
        }
        if (oldVersion < 15) {
            try {
                db.execSQL("ALTER TABLE card ADD version varchar(20) DEFAULT ''");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            try {
                db.execSQL("ALTER TABLE card ADD user_id varchar(20) DEFAULT ''");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            try {
                db.execSQL(CREATE_CARDS_TABLE_COPY);
                db.execSQL("INSERT INTO card_copy (uid, version, card_type, user_id, wallet_info, enable)" +
                        " SELECT uid, version, card_type, user_id, wallet_info, enable FROM card;");
                db.execSQL("DROP TABLE card");
                db.execSQL("ALTER TABLE card_copy RENAME TO card;");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (oldVersion < 16) {
            //增加hd_btc_x_pub字段，表示HD btc钱包的xpub
            db.execSQL("alter table account1 add hd_btc_x_pub varchar(100) DEFAULT '';");
            //增加hd_btc_current_child_address字段，表示HD btc钱包的选中子地址
            db.execSQL("alter table account1 add hd_btc_current_child_address varchar(30) DEFAULT '';");
            //增加hd_btc_child_path_max_index字段，表示HD btc钱包的选中子地址的路径最大index，意思是
            //将要生成这么多个子地址，如"6"表示path"/0/1"....."/0/6"的地址都生成出来
            db.execSQL("alter table account1 add hd_btc_child_path_max_index varchar(10) DEFAULT '';");
        }
    }


}